package com.quanshu.exchange.ui.gui.message.chat.row.viewholder;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.gui.message.chat.row.BaseChatRowViewHolder;
import com.quanshu.exchange.ui.gui.message.chat.row.ChatRowVoicePlayer;

import java.io.File;

// 发送语音消息ViewHolder
public class VoiceSendViewHolder extends BaseChatRowViewHolder {
    ImageView voiceView;
    TextView voiceLength;
    private AnimationDrawable voiceAnimation;
    private ChatRowVoicePlayer voicePlayer;

    public VoiceSendViewHolder(Context context, View view) {
        super(context, view);
        voiceView = view.findViewById(R.id.chat_voice);
        voiceLength = view.findViewById(R.id.chat_voice_size);
        voicePlayer = ChatRowVoicePlayer.getInstance(context);
    }

    @Override
    public void setViewInfo(EMMessage chatMessage, EMMessage preMessage, String groupOwner) {
        super.setViewInfo(chatMessage, preMessage, groupOwner);
        // 设置内容
        EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) chatMessage.getBody();
        int len = voiceBody.getLength();
        if (len > 0) {
            voiceLength.setText(voiceBody.getLength() + "\"");
            voiceLength.setVisibility(View.VISIBLE);
        } else {
            voiceLength.setVisibility(View.INVISIBLE);
        }
        // To avoid the item is recycled by listview and slide to this item again but the animation is stopped.
        ChatRowVoicePlayer voicePlayer = ChatRowVoicePlayer.getInstance(context);
        if (voicePlayer.isPlaying() && chatMessage.getMsgId().equals(voicePlayer.getCurrentPlayingId())) {
            startVoicePlayAnimation();
        }
    }

    public void play(EMMessage message) {
        EMVoiceMessageBody voiceBody = (EMVoiceMessageBody) message.getBody();
        // Play the voice
        String localPath = ((EMVoiceMessageBody) message.getBody()).getLocalUrl();
        File file = new File(localPath);
        if (file.exists() && file.isFile()) {
            playVoice(message);
            // Start the voice play animation.
            startVoicePlayAnimation();
        } else {
            asyncDownloadVoice(message);
        }
    }

    private void playVoice(EMMessage msg) {
        voicePlayer.play(msg, mp -> {
            // Stop the voice play animation.
            stopVoicePlayAnimation();
        });
    }

    public void startVoicePlayAnimation() {
        voiceView.setImageResource(R.drawable.voice_to_playing_anim);
        voiceAnimation = (AnimationDrawable) voiceView.getDrawable();
        voiceAnimation.start();
    }

    public void stopVoicePlayAnimation() {
        if (voiceAnimation != null) {
            voiceAnimation.stop();
        }
        voiceView.setImageResource(R.drawable.chatto_voice_playing_f3);
    }
}